package com.auto.survey.activity.monitoring.ctrlmeasure

import android.graphics.Color
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.auto.survey.R
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_OBJECT_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_PROJET_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_STATION_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_TASK_ID
import com.auto.survey.activity.monitoring.adapter.CtrlSttingAdapter
import com.auto.survey.activity.monitoring.dao.MonitoringRepository
import com.auto.survey.base.BaseActivity
import com.auto.survey.util.Helper
import com.auto.survey.widget.CommonTitle.Companion.TEXT_POSITON_RIGHT
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class CtrlSettingsActivity : BaseActivity(){

    private var projectId:Int = -1
    private var objectId:Int = -1
    private var stationId:Int = -1
    private var taskId:Int = -1
    private val database = MonitoringRepository()

    override fun initView() {
        projectId = intent.getIntExtra(KEY_INTENT_PROJET_ID, -1)
        objectId = intent.getIntExtra(KEY_INTENT_OBJECT_ID, -1)
        stationId = intent.getIntExtra(KEY_INTENT_STATION_ID, -1)
        taskId = intent.getIntExtra(KEY_INTENT_TASK_ID, -1)

        mTopBar?.visibility = View.VISIBLE
        mTopBar?.setBackgroundResource(R.color.action_bar)

        mComment_title?.visibility = View.VISIBLE
        mComment_title?.setBackgroundResource(R.color.action_bar)
        mComment_title?.setTitle("设置")
        mComment_title?.setRightTitle("保存")
        mComment_title?.setTitleColor(Color.WHITE, TEXT_POSITON_RIGHT)
        mComment_title?.setOnRightTextClickListener(Helper.debounce{
            finish()
        })

        val recycler = findViewById<RecyclerView>(R.id.rv_pointHeight_settings)
        recycler.layoutManager = LinearLayoutManager(this)
        val adapter = CtrlSttingAdapter()
        recycler.adapter = adapter

        lifecycleScope.launch {
            val bean = database.getTaskPointList(taskId)
            val points = bean?.monitoringPoints
            points?.let {
                withContext(Dispatchers.Main) {
                    adapter.setItems(it)
                }
            }
        }
    }

    override fun getLayoutId(): Int {

        return R.layout.activity_ctrl_settings
    }

}